﻿using BlockeyEssentials;
using BlockeyEssentials.Drawing;
using BlockeyEssentials.Input;
using BlockeyEssentials.ScreenSystem;
using BlockeyEssentials.ScreenSystem.ScreenTypes;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Blockey.MenuSystemScreens
{
    public class AdjustMenuScreen : MenuScreen
    {
        public AdjustMenuScreen(ScreenManager oManager)
            : base(oManager, "Adjust Menu")
        {
            for (MenuItem oOption = MenuItem.Example; oOption <= MenuItem.Back; oOption++)
            {
                _Options.Add(oOption.ToString().ToUpper());
            }

            int oWidth = 700;
            int oBuffer = 60;
            int oInsideBuffer = 20;
            int oBackHeight = 100;

            _TextFont = new Font(All.Font_Debug);
            _TextFont.Color = Color.Black;
            _TextFont.Layer = 95;

            var oBounds = new Rectangle(
                All.Resolution.Center.X - (oWidth / 2) - oInsideBuffer,
                oBuffer,
                oWidth,
                All.Resolution.Bottom - (oBuffer * 2));

            var oShape = new Rectangle(
                oBounds.Left + oInsideBuffer,
                oBounds.Top + oInsideBuffer,
                (oBounds.Width - oInsideBuffer) / 2,
                (oBounds.Height - (oInsideBuffer * 4) - oBackHeight) / 2);

            //-------- Example Button --------//
            _ExampleButton = new MenuButton(oShape, Color.Red, 80, _TextFont, " Example");

            //-------- Back Button --------//
            oShape.Y = oShape.Bottom + oInsideBuffer;
            oShape.Width = oBounds.Width;
            oShape.X = oBounds.Left + oInsideBuffer;
            oShape.Height = oBackHeight;
            _BackButton = new MenuButton(oShape, Color.Crimson, 80, _TextFont, "Back");

            //-------- Selected Item Highlight --------//
            _SelectedItemHighlight = new SpriteR(All.Texture_Square, _ExampleButton.Bounds);
            _SelectedItemHighlight.Color = Color.Yellow;
            _SelectedItemHighlight.Layer = 90;
        }

        #region Methods

        public override void Update()
        {
            if (Active)
            {
                Input();
            }
        }

        public override void Draw(SpriteBatch oSB)
        {
            _ExampleButton.Draw(oSB, Transition);
            _BackButton.Draw(oSB, Transition);
            _SelectedItemHighlight.Draw(oSB, Transition);
        }

        protected override void Select()
        {
            switch ((MenuItem)_Index)
            {
                case MenuItem.Example:
                    {
                        break;
                    }
                case MenuItem.Back:
                    {
                        Cancel();
                        break;
                    }
            }
        }

        protected override void Cancel()
        {
            var oScreen = new MainMenuScreen(_Manager);
            GoTo(oScreen);
        }

        protected override void IndexTo(int oIndex)
        {
            base.IndexTo(oIndex);
            ChangeSelectedHighlight();
        }

        #region Helper Methods

        private void Input()
        {
            var oInput = InputManager.Keyboard;

            if (oInput.JustPressed(Keys.Up))
            {
                return;
            }

            if (oInput.JustPressed(Keys.Down))
            {
                return;
            }

            if (oInput.JustPressed(Keys.Right))
            {
                return;
            }
            if (oInput.JustPressed(Keys.Left))
            {
                return;
            }

            if (oInput.JustPressed(Keys.Enter, Keys.F))
            {
                Select();
                return;
            }

            if (oInput.JustPressed(Keys.D))
            {
                Cancel();
                return;
            }

        }

        private void ChangeSelectedHighlight()
        {
            switch ((MenuItem)_Index)
            {
                case MenuItem.Example: _SelectedItemHighlight.Rectangle = _ExampleButton.Bounds;
                    break;
                case MenuItem.Back: _SelectedItemHighlight.Rectangle = _BackButton.Bounds;
                    break;
            }
        }

        #endregion

        #endregion

        private MenuButton _ExampleButton;
        private MenuButton _BackButton;

        private SpriteR _SelectedItemHighlight;
        private MenuItem _LastIndex;

        private Font _TextFont;

        public enum MenuItem
        {
            Example,
            Back
        }

    }
}
